package com.mz.ozy.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.mz.ozy.R;
import com.mz.ozy.bean.MyOrderBean;
import com.mz.ozy.bean.MyOrderInfoBean;
import com.mz.ozy.utils.FullyGridLayoutManager;
import com.mz.ozy.utils.glideUtils.GlideUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Administrator on 2018/11/23.
 */

public class EvauInfoMainAdapter extends BaseQuickAdapter<MyOrderBean.OrderBean.PageItemsBean.OrderItemsBean, BaseViewHolder> {
    private final Activity mActivity;
    private int maxSelectNum = 9;
    private List<List<String>> mAdapterImgArr = new ArrayList<>();
    private EvauInfoMainAdapter.onAddPicClickListener mOnAddPicClickListener;

    public EvauInfoMainAdapter(Activity activity, @Nullable List<MyOrderBean.OrderBean.PageItemsBean.OrderItemsBean> data) {
        super(R.layout.view_item_my_evau, data);
        this.mActivity = activity;
        for (int i = 0; i < data.size(); i++) {
            mAdapterImgArr.add(new ArrayList<>());
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, MyOrderBean.OrderBean.PageItemsBean.OrderItemsBean item) {
        RadioGroup radioGroup = helper.getView(R.id.pay_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                View checkedView = group.findViewById(checkedId);
                int tag = Integer.valueOf(checkedView.getTag().toString());

                mOnAddPicClickListener.setSelectedNum(tag);
            }
        });
        EditText et_evaluate = helper.getView(R.id.et_evaluate);
        mOnAddPicClickListener.setSelectedNum1(et_evaluate.getText().toString());

        helper.addOnClickListener(R.id.btn_refund);
        ImageView ivIcon = helper.getView(R.id.iv_icon);
        EditText mEtEvaluate = helper.getView(R.id.et_evaluate);
        RecyclerView mRvPhoto = helper.getView(R.id.rv_photo);
       /* helper.setText(R.id.tv_title, item.getGoodstype())
                .setText(R.id.tv_info, "null".equals(item.getArrribute_name()) ? "" : item.getArrribute_name())
                .setText(R.id.tv_count, "×" + item.getNum())
                .setText(R.id.tv_money, getPrice(item));*/
        final int adapterPosition = helper.getAdapterPosition();
        EvaluatePhotoAdapter mAdapter = new EvaluatePhotoAdapter(mAdapterImgArr.get(adapterPosition), R.mipmap.post_msg_plus_pic);
        mAdapter.setOnAddPicClickListener(new ReleaseMsgAdapter.onAddPicClickListener() {
            @Override
            public void onAddPicClick() {
                mOnAddPicClickListener.onAddPicClick(adapterPosition);
                PictureSelector.create((Activity) mActivity)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(maxSelectNum - mAdapter.getData().size())
                        .minSelectNum(1)
                        .imageSpanCount(4)
                        .selectionMode(PictureConfig.MULTIPLE)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }

            @Override
            public void onRemovePicClick(int index) {
                mOnAddPicClickListener.onRemovePicClick(adapterPosition, index);

//                img.remove(index);
            }
        });
        mRvPhoto.setHasFixedSize(true);
        FullyGridLayoutManager gridLayoutManager = new FullyGridLayoutManager(mActivity, 4, GridLayoutManager.VERTICAL, false);
        mRvPhoto.setLayoutManager(gridLayoutManager);
        mAdapter.setSelectMax(maxSelectNum);
        mRvPhoto.setAdapter(mAdapter);

        if (!TextUtils.isEmpty(item.getImg_zt())) {
            final String[] split = item.getImg_zt().split(",");
            Glide.with(mContext).load(split[0]).into(ivIcon);
        } else {
            GlideUtils.loadImg(mContext, ivIcon, R.mipmap.logo);
        }
    }

    public void setOnAddPicClickListener(EvauInfoMainAdapter.onAddPicClickListener onAddPicClickListener) {
        mOnAddPicClickListener = onAddPicClickListener;
    }

    public void addAdapterImg(int position, String path) {
        final List<String> imgArr = mAdapterImgArr.get(position);
        imgArr.add(path);
        notifyDataSetChanged();
    }

    public void removeAdapterImg(int position, int index) {
        final List<String> imgArr = mAdapterImgArr.get(position);
        imgArr.remove(index);
        notifyDataSetChanged();
    }

    public interface onAddPicClickListener {
        void onAddPicClick(int adapterPosition);

        void onRemovePicClick(int adapterPosition, int index);

        void setSelectedNum(int num);

        void setSelectedNum1(String num);


    }

    @NotNull
    private String getPrice(MyOrderInfoBean.OrdermapBean.ProdcutListBean item) {
        String price = "";
        if (!TextUtils.isEmpty(item.getPrice()) && Double.parseDouble(item.getPrice()) != 0.00) {
            price += item.getPrice();
            price += "KT";
        }
        if (!TextUtils.isEmpty(item.getMarket_price()) && Double.parseDouble(item.getMarket_price()) != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += item.getMarket_price();
                price += "消费积分";
            } else {
                price += item.getMarket_price();
                price += "消费积分";
            }
        }
        if (TextUtils.isEmpty(price)) {
            price = "0.00KT";
        }
        return price;
    }

}
